package pl.arekczogala.plati;


import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import android.util.Log;
/**
 * @author maker
 *
 */
public class Plati extends Activity implements SensorEventListener {

	private TextView status;

	private Button btn_connect;
	private Button btn_start;
	
	private EditText addressIP;
	
	ProgressDialog progressDialog;
	
	private Editable adrIP;
	private InetAddress remoteIP;
	private Integer portTCP;
	
	private Boolean send_data = false;
	
	private String data_or = "null",data_acc = "null",data = "null";
	
	private String IP;
	
	private ConnectionTCP conn;
	//NetworkUDP conn;
	
	private SensorManager mSensorManager;
    private Sensor accSensor;
    //private Sensor orSensor;
    
    //private PowerManager pm;
    //private PowerManager.WakeLock wl;
	
	boolean play = false;

	
	

	
	
	
	
	
	
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    setContentView(R.layout.main);
	    //find ui elements
	    btn_connect = (Button)findViewById(R.id.btn_connect);
	    btn_start = (Button)findViewById(R.id.btn_start);
	    status = (TextView)findViewById(R.id.status);
	    addressIP = (EditText)findViewById(R.id.addressIP);
	    
	    progressDialog = new ProgressDialog(Plati.this);
	    progressDialog.setTitle("Connecting");
	    progressDialog.setMessage("Please wait...");
	    progressDialog.setCancelable(true);
	           
	    //lock screen orientation
	    //this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	    
	    //pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
	    //wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "Plati");
	    
	    mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
	    //type orientation is not working under emulator - exits unexpectatly
	    
	   
	    //orSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
	    accSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		
		//register btn's		
		btn_connect.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){	
				
				Log.v("tag", "connect ");
				
				progressDialog.show();
				IP = addressIP.getText().toString();
				 //TODO: get port number from field
				portTCP = 65000;
				try{
					remoteIP = InetAddress.getByName(IP);
					conn = new ConnectionTCP(remoteIP,portTCP);
				}
				catch (UnknownHostException e) {
		            // TODO Auto-generated catch block
		            e.getMessage();
		            progressDialog.setMessage("meee");
				}
				if(!conn.connect()){
					
					progressDialog.setMessage(conn.err_msg);
				}
				//progressDialog.hide();
				
				//status.setText(conn.connect(IP));
			}
		});
		
		btn_start.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){			
				
				if(!send_data){			
					send_data = true;
					
					//wl.acquire();
					
					btn_start.setText("Stop");
					data = "Started transmiting data.";
					conn.send(data);
				}
				else{
					send_data = false;					
					//wl.release();					
					btn_start.setText("Start");
					data = "Stoped transmiting data.";
					conn.send(data);
				}
				
			}
		});

	    // TODO Auto-generated method stub
	}

    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_GAME);
        //mSensorManager.registerListener(this, orSensor, SensorManager.SENSOR_DELAY_GAME);
    }

    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }
    
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
    	
    	if(event.sensor.getType() == accSensor.getType()){
    		//x_acc_value.setText(String.valueOf(event.values[0]));
           // y_acc_value.setText(String.valueOf(event.values[1]));
           // z_acc_value.setText(String.valueOf(event.values[2]));
            data_acc = String.valueOf(event.values[0])+";"+String.valueOf(event.values[1])+";"+String.valueOf(event.values[2])+"|";
    	}
    	/*
    	if(event.sensor.getType() == orSensor.getType()){
    		//x_or_value.setText(String.valueOf(event.values[0]));
           // y_or_value.setText(String.valueOf(event.values[1]));
            //z_or_value.setText(String.valueOf(event.values[2]));
            data_or = String.valueOf(event.values[0])+";"+String.valueOf(event.values[1])+";"+String.valueOf(event.values[2])+";";
    	}
         */
        if(send_data){
        	//conn.send(data_or+data_acc);
        } 	

    }

    @Override
    protected void onStop() {
    	mSensorManager.unregisterListener(this);
        super.onStop();
    }
}
